package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        //执行分页查询
        Page<Setmeal> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        //保存套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入数据获取回显之后的id
        setmealMapper.saveBaseWithSetmeal(setmeal);
        Long id = setmeal.getId();
        log.info("套餐id:{}", id);


        //保存套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //循环便利插入 套餐id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        setmealMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Integer id) {
        setmealMapper.startOrStop(status, id);
    }

    /**
     * 根据id查询套餐数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐基本信息
        SetmealVO setmealVO = setmealMapper.getByIdWithDishBase(id);

        //查询套餐基本信息
        List<SetmealDish> setmealDishes = setmealMapper.getByIdDishExtendtion(id);

        //组合数据
        setmealVO.setSetmealDishes(setmealDishes);

        //查询套餐菜品信息
        return setmealVO;
    }

    /**
     * 根据id删除套餐数据
     *
     * @param ids
     */
    @Override
    public void deleteByIdWithdish(List<Integer> ids) {
        //删除套餐信息
        setmealMapper.deleteByIdWithSetmeal(ids);
        //删除套餐关联的菜品信息
        setmealMapper.deleteByIdWithSetmealDish(ids);
    }

    /**
     * 修改套餐数据
     *
     * @param setmealDTO
     */
    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐基本信息
        setmealMapper.updateById(setmeal);

        //删除套餐关联的菜品信息
        List<Integer> ids = new ArrayList<>();
        if (setmeal.getId() != null) {
            ids.add(setmeal.getId().intValue());
        }
        log.info("ids:{}", ids);

        setmealMapper.deleteByIdWithSetmealDish(ids);

        //保存套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setmealMapper.insertBatch(setmealDishes);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
